package com.mercenary.wein.screenControl.deviceControll;

import com.android.ddmlib.*;
import com.mercenary.wein.screenControl.ScreenPrint.ScreenCaptureThread;
import org.apache.commons.logging.*;
import org.apache.commons.logging.Log;

import java.io.File;
import java.io.IOException;

/**
 * Created by tuxiao on 2017/2/26.
 */
public class AndroidDevice {
    Log log = LogFactory.getLog(AndroidDevice.class);
    private IDevice device;
    private ScreenCaptureThread screenCaptureThread;
    private boolean ifRefresh = true;

    public ScreenCaptureThread getScreenCaptureThread() {
        return screenCaptureThread;
    }

    public void setScreenCaptureThread(ScreenCaptureThread screenCaptureThread) {
        this.screenCaptureThread = screenCaptureThread;
    }


    public String getSerialNo() {
        if (device == null) return "undefined";
        return device.getSerialNumber();
    }

    public String getManufacturer() {
        if (device == null) return "undefined";
        return device.getProperty("ro.product.manufacturer");
    }

    public String getProductModel() {
        if (device == null) return "undefined";
        return device.getProperty("ro.product.model");
    }

    public IDevice getDevice() {
        return device;
    }

    public void setDevice(IDevice device) {
        this.device = device;
    }

    public AndroidDevice(IDevice device) {
        setDevice(device);
        setScreenCaptureThread(new ScreenCaptureThread(device));
    }

    public void stopScreenCaptureThread() {
        getScreenCaptureThread().setFlag(false);
    }

    public void installAPK(String absPath) {
        try {
            device.installPackage(absPath, true);
        } catch (InstallException e) {
            e.printStackTrace();
        }
    }

    public void createForward(int fport, int tport) {
        try {
            device.createForward(fport, tport);
        } catch (TimeoutException e) {
            e.printStackTrace();
        } catch (AdbCommandRejectedException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void removeForward(int fport, int tport) {
        try {
            device.removeForward(fport, tport);
        } catch (TimeoutException e) {
            e.printStackTrace();
        } catch (AdbCommandRejectedException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void executeShellCommand(String command, IShellOutputReceiver receiver) {
        try {
            device.executeShellCommand(command, receiver);
        } catch (TimeoutException e) {
            e.printStackTrace();
        } catch (AdbCommandRejectedException e) {
            e.printStackTrace();
        } catch (ShellCommandUnresponsiveException e) {
//            e.printStackTrace();
            log.info("会报错，但是文件正常pull了");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void pullFile(String remote, String local) {
        try {
            device.pullFile(remote, local);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (AdbCommandRejectedException e) {
            e.printStackTrace();
        } catch (TimeoutException e) {
            e.printStackTrace();
        } catch (SyncException e) {
            e.printStackTrace();
        }
    }

    public void pushFile(File localFrom, String remoteTo) {
        try {
            if (device.getSyncService() == null)
                throw new RuntimeException("SyncService is null, ADB crashed ?");

            device.getSyncService().pushFile(localFrom.getAbsolutePath(),
                    remoteTo, new SyncService.ISyncProgressMonitor() {
                        public void start(int i) {

                        }

                        public void stop() {

                        }

                        public boolean isCanceled() {
                            return false;
                        }

                        public void startSubTask(String s) {

                        }

                        public void advance(int i) {

                        }
                    });
        } catch (Exception ex) {
            throw new RuntimeException(ex);
        }
    }
}
